package panel.home;


import javax.swing.*;
import java.awt.*;

public class ChatMessage extends JPanel {
    private final int Max_width = 450;
    private final int en_chat_len = 15;  //字体大小为30的字母占用宽度
    private final int cn_chat_len = 30;  //字体大小为30的汉字占用宽度
    private int max_len;
    private int lay = 0;

    JTextArea jTextArea = new JTextArea();
    JLabel label;

    public ChatMessage(ImageIcon imageIcon) {
        label = new JLabel(imageIcon);
        performImage(imageIcon);
    }

    public ChatMessage(String string) {
        performText(string);
    }

    private void init() {
        setLayout(null);
        setBackground(new Color(50, 185, 230, 180));
        setLocation(50, 50);
    }

    public void performImage(ImageIcon imageIcon) {
        add(label);
        init();
        if (imageIcon.getIconWidth() > Max_width) {
            int height = imageIcon.getIconHeight();
            int width = imageIcon.getIconWidth();
            height = height * Max_width / width;
            Image newimage = imageIcon.getImage().getScaledInstance(Max_width, height, Image.SCALE_SMOOTH);
            ImageIcon imageIconNew = new ImageIcon(newimage);
            label.setIcon(imageIconNew);
        } else {
            label.setIcon(imageIcon);
        }
        label.setSize(label.getIcon().getIconWidth(), label.getIcon().getIconHeight());
        label.setLocation(10, 10);
        setSize(label.getWidth() + 20, label.getHeight() + 20);
    }

    public void performText(String string) {
        init();
        this.setLocation(0, 0);
//        this.setBackground(Color.BLACK);
        this.setLayout(null);
        jTextArea.setFont(new Font("宋体", Font.PLAIN, 30));
        jTextArea.setLocation(0, 0);
        jTextArea.setText(string);
        jTextArea.setOpaque(false);
        jTextArea.setLocation(10, 10);
        set(string);
        this.add(jTextArea);
        //jTextArea.setText(string);
        setSize(jTextArea.getWidth() + 20, jTextArea.getHeight() + 20);
    }

    public void set(String string) {
        length(string);
        if (max_len <= Max_width) {
            jTextArea.setSize(max_len, 35 * lay);
        } else {
            jTextArea.setSize(Max_width, 35 * lay);
        }
        jTextArea.setEditable(false);
        jTextArea.setLineWrap(true);
        jTextArea.setVisible(true);
    }


    public void length(String value) {
        int valueLength = 0;
        System.out.print(value);
        String chinese = "[\u0391-\uFFE5]";

        for (int i = 0; i < value.length(); i++) {
            // 获取一个字符
            String temp = value.substring(i, i + 1);
            // 判断是否为中文字符
            if (temp.matches(chinese)) {
                valueLength += cn_chat_len;
            } else {
                valueLength += en_chat_len;
            }
            if (temp.equals("\n")) {
                if (valueLength > max_len) {
                    max_len = valueLength;
                }
                lay += valueLength / Max_width;
                int left = valueLength - (Max_width * lay);
                if (left > 0) {
                    lay++;
                }
                lay++;
                valueLength = 0;
            }
        }

        if (valueLength > max_len) {
            max_len = valueLength;
        }
        lay += valueLength / Max_width;
        int left = valueLength - (Max_width * lay);
        if (left > 0) {
            lay++;
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(getBackground());
        g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
    }
}